package cn.com.cfae.iras.doc.analyze.redis;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.Collection;


public class RedisUtil {


    private static final Logger logger = LoggerFactory.getLogger(RedisUtil.class);
    private RedisTemplate redisTemplate;


    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
        initSerializer();
    }


    private void initSerializer() {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
    }

    public void lpush(String key, String value) {
        if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(value))
            redisTemplate.opsForList().leftPush(key, value);
    }

    public void lpush(String key, String... values) {
        if (StringUtils.isNotEmpty(key) && ArrayUtils.isNotEmpty(values))
            redisTemplate.opsForList().leftPush(key, values);
    }

    public boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    public boolean deleteKey(String key) {
        redisTemplate.delete(key);
        return true;
    }

    public boolean deleteKey(Collection collection) {
        redisTemplate.delete(collection);
        return true;
    }


    public void lpush(String key, Collection collection) {
        if (StringUtils.isNotEmpty(key) && CollectionUtils.isNotEmpty(collection))
            redisTemplate.opsForList().leftPushAll(key,collection);
    }


}
